<!-- 手机号换绑弹窗 -->
<template>
  <el-dialog
    width="750px"
    :visible="visible"
    :lock-scroll="false"
    :destroy-on-close="true"
    custom-class="ele-dialog-form"
    :title="'手机号换绑'"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :sm="12">
          <el-form-item label="旧手机号:">
            <el-input disabled v-model="form.phoneText" />
          </el-form-item>
        </el-col>
        <el-col :sm="12">
          <el-form-item label="新手机号" prop="newphone">
            <el-input v-model="form.newphone" placeholder="请输入新手机号" />
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              placeholder="请输入备注"
              type="textarea"
              v-model="form.remark"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消 </el-button>
      <el-button type="primary" :loading="loading" @click="save"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'MemberEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      form: Object.assign({ status: 1, gender: 1 }, this.data),
      // 表单验证规则
      rules: {
        newphone: [
          { required: true, message: '请收入新手机号', trigger: 'blur' }
        ],
        remark: [{ required: true, message: '请输入备注', trigger: 'blur' }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 会员等级列表
      memberLevelList: []
    }
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data)
        console.log(this.form)
        this.$set(this.form, 'phoneText', this.phoneText(this.form.phone))
        this.isUpdate = true
      } else {
        this.form = {}
        this.isUpdate = false
      }
    }
  },
  methods: {
    /* 保存编辑 */
    phoneText(val) {
      console.log(val)
      if (val) {
        let tel = val.substr(0, 3) + '****' + val.substr(7)
        return tel
      } else {
        return ''
      }
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const saveform = {
            id: this.form.id,
            phone: this.form.newphone,
            remark: this.form.remark
          }
          this.$http
            .post('/Member/setMemberPhone', saveform)
            .then((res) => {
              this.loading = false
              if (res.data.code === 0) {
                this.$message({ type: 'success', message: res.data.msg })
                if (!this.isUpdate) {
                  this.form = {}
                }
                this.updateVisible(false)
                this.$emit('done')
              } else {
                this.$message.error(res.data.msg)
              }
            })
            .catch((e) => {
              this.loading = false
              this.$message.error(e.message)
            })
        } else {
          return false
        }
      })
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value)
    }
  }
}
</script>

<style scoped></style>
